import requests
import json
import os
from time import sleep
from config.configs import STOCKINORDERS_LIST_URL, ORDERS_LIST_URL, HEADER, RETURN_ORDERS_LIST_URL, STOCK_DETAIL_URL \
    , DATAS_BOARD_ARTICLENUMBER_URL, SETTLEMENT_UPLOAD_URL, SETTLEMENT_CONFIRM_URL, DATA_PATH, SETTLEMENT_LIST_URL


def upload_settlement():
    """workflow测试环境上传结算"""
    file_path = os.path.join(DATA_PATH, 'wf_settlement_upload.xlsx')
    file = {'file': open(file_path, mode='rb')}
    r = requests.post(url=SETTLEMENT_UPLOAD_URL, headers=HEADER, files=file)
    sleep(3)
    result = json.loads(r.text)
    data = {"settlementMethod": "手动上传", "settlementList": result['data']}
    requests.post(url=SETTLEMENT_CONFIRM_URL, headers=HEADER, json=data)
    sleep(7)


def get_origin_stockinorders():
    """获取入库单数据"""
    r = requests.get(url=STOCKINORDERS_LIST_URL, headers=HEADER)
    re = json.loads(r.text)
    with open('../data/wf_origin_stockinorders.json', 'wt', encoding='utf-8') as f:
        json.dump(re["data"]["list"], f, ensure_ascii=False, indent=2)


def get_origin_stocks():
    """获取炜哥香港自送_AP2309199715782_10025239库存数据"""
    r = requests.get(url=STOCK_DETAIL_URL, headers=HEADER)
    re = json.loads(r.text)
    with open('../data/wf_origin_stocks.json', 'wt', encoding='utf-8') as f:
        json.dump(re["data"]["stockInProducts"], f, ensure_ascii=False, indent=2)


def get_origin_order():
    """获取结算前订单数据"""
    r = requests.get(url=ORDERS_LIST_URL, headers=HEADER)
    result = json.loads(r.text)
    with open('../data/wf_origin_orders.json', 'wt', encoding='utf-8') as f:
        json.dump(result["data"]["list"], f, ensure_ascii=False, indent=2)


def get_origin_return_order():
    """获取结算前退货订单数据"""
    r = requests.get(url=RETURN_ORDERS_LIST_URL, headers=HEADER)
    result = json.loads(r.text)
    with open('../data/wf_origin_return_orders.json', 'wt', encoding='utf-8') as f:
        json.dump(result["data"]["list"], f, ensure_ascii=False, indent=2)


def get_origin_datas_board_list():
    """获取货号维度数据面板表格"""
    r = requests.get(url=DATAS_BOARD_ARTICLENUMBER_URL, headers=HEADER)
    result = json.loads(r.text)
    with open('../data/wf_origin_datas_board_list.json', 'wt', encoding='utf-8') as f:
        json.dump(result["data"]["list"], f, ensure_ascii=False, indent=2)


def get_origin_datas_sumup():
    """获取货号维度数据面板透视表"""
    r = requests.get(url=DATAS_BOARD_ARTICLENUMBER_URL, headers=HEADER)
    result = json.loads(r.text)
    with open('../data/wf_origin_datas_board_sumup.json', 'wt', encoding='utf-8') as f:
        json.dump(result["data"]["sumup"], f, ensure_ascii=False, indent=2)


def get_new_order():
    """获取结算后订单数据"""
    r = requests.get(url=ORDERS_LIST_URL, headers=HEADER)
    result = json.loads(r.text)
    with open('../data/wf_new_orders.json', 'wt', encoding='utf-8') as f:
        json.dump(result["data"]["list"], f, ensure_ascii=False, indent=2)


def get_new_return_order():
    """获取结算后退货订单数据"""
    r = requests.get(url=RETURN_ORDERS_LIST_URL, headers=HEADER)
    result = json.loads(r.text)
    with open('../data/wf_new_return_orders.json', 'wt', encoding='utf-8') as f:
        json.dump(result["data"]["list"], f, ensure_ascii=False, indent=2)


def get_settlements():
    """获取结算列表数据"""
    re = requests.get(url=SETTLEMENT_LIST_URL, headers=HEADER)
    if re.json()['data']['total'] == 0:
        upload_settlement()
        r = requests.get(url=SETTLEMENT_LIST_URL, headers=HEADER)
        result = json.loads(r.text)
        with open('../data/wf_settlements.json', 'wt', encoding='utf-8') as f:
            json.dump(result["data"]["list"], f, ensure_ascii=False, indent=2)
    else:
        r = requests.get(url=SETTLEMENT_LIST_URL, headers=HEADER)
        result = json.loads(r.text)
        with open('../data/wf_settlements.json', 'wt', encoding='utf-8') as f:
            json.dump(result["data"]["list"], f, ensure_ascii=False, indent=2)


def get_all_origin_datas():
    get_settlements()
    get_origin_datas_sumup()
    get_origin_stockinorders()
    get_origin_order()
    get_origin_datas_board_list()
    get_origin_return_order()
    get_origin_stocks()
    get_new_order()
    get_new_return_order()
